// 轮播图功能
document.addEventListener('DOMContentLoaded', function() {
    const slides = document.querySelectorAll('.carousel-slide');
    const dots = document.querySelectorAll('.carousel-dot');
    const prevBtn = document.querySelector('.carousel-control.prev');
    const nextBtn = document.querySelector('.carousel-control.next');
    const slidesContainer = document.querySelector('.carousel-slides');
    
    // 只有在存在轮播图元素时才初始化轮播功能
    if (slides.length > 0 && dots.length > 0 && prevBtn && nextBtn && slidesContainer) {
        let currentSlide = 0;
        let slideInterval;
        
        // 初始化轮播图
        function initCarousel() {
            showSlide(currentSlide);
            startSlideInterval();
            
            // 为轮播指示器添加点击事件
            dots.forEach((dot, index) => {
                dot.addEventListener('click', () => {
                    goToSlide(index);
                });
            });
            
            // 为控制按钮添加点击事件
            prevBtn.addEventListener('click', () => {
                goToSlide(currentSlide - 1);
            });
            
            nextBtn.addEventListener('click', () => {
                goToSlide(currentSlide + 1);
            });
            
            // 鼠标悬停时暂停轮播
            const carouselControls = document.querySelectorAll('.carousel-control, .carousel-dot');
            carouselControls.forEach(control => {
                control.addEventListener('mouseenter', pauseSlideInterval);
                control.addEventListener('mouseleave', startSlideInterval);
            });
        }
        
        // 显示指定幻灯片
        function showSlide(index) {
            if (index < 0) index = slides.length - 1;
            if (index >= slides.length) index = 0;
            
            slidesContainer.style.transform = `translateX(-${index * 100}%)`;
            
            // 更新指示器状态
            dots.forEach((dot, i) => {
                dot.classList.toggle('active', i === index);
            });
            
            currentSlide = index;
        }
        
        // 切换到指定幻灯片
        function goToSlide(index) {
            pauseSlideInterval();
            showSlide(index);
            startSlideInterval();
        }
        
        // 开始自动轮播
        function startSlideInterval() {
            slideInterval = setInterval(() => {
                goToSlide(currentSlide + 1);
            }, 3000);
        }
        
        // 暂停自动轮播
        function pauseSlideInterval() {
            clearInterval(slideInterval);
        }
        
        initCarousel();
    }
    
    // 移动端菜单
    const menuToggle = document.getElementById('menu-toggle');
    const mobileMenu = document.getElementById('mobile-menu');
    
    if (menuToggle && mobileMenu) {
        menuToggle.addEventListener('click', () => {
            mobileMenu.classList.toggle('active');
        });
    }
    
    // 移动端下拉菜单
    const mobileDropdownToggles = document.querySelectorAll('.mobile-dropdown-toggle');
    
    mobileDropdownToggles.forEach(toggle => {
        toggle.addEventListener('click', () => {
            const dropdown = toggle.nextElementSibling;
            dropdown.classList.toggle('active');
            
            // 切换图标
            const icon = toggle.querySelector('i');
            icon.classList.toggle('fa-chevron-down');
            icon.classList.toggle('fa-chevron-up');
        });
    });
    
    // 导航栏滚动效果
    const navbar = document.getElementById('navbar');
    let lastScrollTop = 0;
    
    if (navbar) {
        window.addEventListener('scroll', () => {
            const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
            
            if (scrollTop > 100) {
                navbar.classList.add('sticky');
            } else {
                navbar.classList.remove('sticky');
            }
            
            lastScrollTop = scrollTop;
        });
    }
    
    // 返回顶部按钮
    const backToTopBtn = document.getElementById('back-to-top');
    
    if (backToTopBtn) {
        window.addEventListener('scroll', () => {
            if (window.pageYOffset > 300) {
                backToTopBtn.classList.add('visible');
            } else {
                backToTopBtn.classList.remove('visible');
            }
        });
        
        backToTopBtn.addEventListener('click', () => {
            window.scrollTo({
                top: 0,
                behavior: 'smooth'
            });
        });
    }
    
    // 淡入动画
    const fadeElements = document.querySelectorAll('.carousel, section');
    
    if (fadeElements.length > 0) {
        const fadeInObserver = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    entry.target.classList.add('fade-in');
                    fadeInObserver.unobserve(entry.target);
                }
            });
        }, { threshold: 0.1 });
        
        fadeElements.forEach(element => {
            fadeInObserver.observe(element);
        });
    }
    
    // 订阅表单
    const subscribeForm = document.getElementById('subscribe-form');
    
    if (subscribeForm) {
        subscribeForm.addEventListener('submit', (e) => {
            e.preventDefault();
            const emailInput = subscribeForm.querySelector('input[type="email"]');
            const email = emailInput.value.trim();
            
            if (email === '') {
                alert('请输入您的邮箱地址');
                return;
            }
            
            // 简单的邮箱格式验证
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if (!emailRegex.test(email)) {
                alert('请输入有效的邮箱地址');
                return;
            }
            
            // 模拟提交成功
            alert('订阅成功！感谢您的关注');
            emailInput.value = '';
        });
    }
}); 